package com.atguigu.eduservice.controller;


import com.aiguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.dto.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-09-24
 */
@Api(description = "章节控制器")
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Resource
    private EduChapterService eduChapterService;

    /**
     * 根据课程id查询
     * @param courseId 课程id
     * @return
     */
    @ApiOperation("查询章节小结树形结构")
    @GetMapping("/getCharterVideo/{courseId}")
    public R getCharterVideo(
            @ApiParam(value = "课程id", required = true)
            @PathVariable String courseId) {
        List<ChapterVo> list = eduChapterService.getCharterVideoById(courseId);
        return R.ok().data("info", list);
    }

    @ApiOperation("保存章节信息")
    @PostMapping("/saveEntity")
    public R saveEntity(
            @ApiParam(name = "eduChapter", value = "章节对象", required = true)
            @RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    @ApiOperation("修改章节信息")
    @PutMapping("/updateEntity")
    public R updateEntity(
            @ApiParam(name = "eduChapter", value = "章节对象", required = true)
            @RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    @ApiOperation("根据id删除实体信息")
    @DeleteMapping("/deleteEntityById/{id}")
    public R deleteEntityById(@PathVariable("id") String id) {
        return  eduChapterService.deleteEntityById(id) ? R.ok() :R.error();
    }
    @ApiOperation("根据id获取章节信息")
    @GetMapping("/getChapterInfo/{id}")
    public R getChapterInfo(
            @ApiParam("章节id")
        @PathVariable("id") String id
    ){
        return R.ok().data("info", eduChapterService.getById(id));
    }
}

